package com.vanzo.talkie;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by tisen on 2018/2/8.
 */

public class StateImage extends ImageView {
    public StateImage(Context context) {
        super(context);
    }

    public StateImage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public StateImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public StateImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    public static final int STATE_ON = 0;
    public static final int STATE_OFF = 1;
    public static final int STATE_ENABLE = 2;

    private int state = 2;
    private int state_on_src;
    private int state_off_src;
    private int state_enable_src;

    public void setSrc(int state_on_src, int state_off_src, int state_enable_src) {
        this.state_on_src = state_on_src;
        this.state_off_src = state_off_src;
        this.state_enable_src = state_enable_src;
    }

    public interface OnClickListener {
        void onONClick(View view);

        void onOFFClick(View view);
    }

    private OnClickListener onClickListener;

    public void setStatusClickListener(OnClickListener mOnClickListener) {
        this.onClickListener = mOnClickListener;
        this.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    switch (getState()) {
                        case STATE_ENABLE:
                            break;
                        case STATE_ON:
                            onClickListener.onONClick(v);
                            break;
                        case STATE_OFF:
                            onClickListener.onOFFClick(v);
                            break;
                    }
                }
            }
        });
    }


    public void setState(int state) {
        switch (state) {
            case STATE_ON:
                this.state = state;
                setClickable(true);
                setImageResource(state_off_src);
                break;
            case STATE_OFF:
                setClickable(true);
                this.state = state;
                setImageResource(state_on_src);
                break;
            case STATE_ENABLE:
                setClickable(false);
                this.state = state;
                setImageResource(state_enable_src);
                break;
        }
    }

    public int getState() {
        return state;
    }
}
